<?
include "include/config.php";
$location='oferte';

if(!isset($_SESSION['_user'])) { 
header("location: login.php");
}

$loc=$_REQUEST['loc'];
$tip=array ('0'=>'Indiferent','1'=>'Garsoniera','2'=>'Apartament 2 camere','3'=>'Apartament 3 camere','4'=>'Apartament 4 camere','5'=>'Teren','6'=>'Spatiu Comercial','7'=>'Casa/Vila');
$valuta=array ('0'=>'Discutabil','1'=>'RON','2'=>'EURO','3'=>'USD');
$oferta=array ('0'=>'Indiferent','1'=>'Vanzare','2'=>'Inchiriere');

if($_SESSION['_level']==0) { 
    if ($_GET['sterge']!='') {
	deleteImage($_GET['sterge']);
	mysql_query("delete from oferta where id='".$_GET['sterge']."'");
	
    }
}

if($_SESSION['_level']!=0) { 
    if ($_GET['sterge']!='') {
	deleteImage($_GET['sterge']);
	mysql_query("delete from oferta where id='".$_GET['sterge']."' and user_id= ".$_SESSION["_id"] ." ");	
    }
}


function deleteImage($iOfertaId) {
    $row_query=mysql_query("SELECT * FROM `imagini` WHERE id_oferta=$iOfertaId ");
    while($row=mysql_fetch_array($row_query)) {
	unlink(substr(srvd, 0, -6)."imagini/thumbs/".$row['src']);
	unlink(substr(srvd, 0, -6)."imagini/normale/".$row['src']);
	unlink(substr(srvd, 0, -6)."imagini/mari/".$row['src']);
    }	
}


if($_SESSION['_level']==0) {
	$sWhere= "  ";
} else {
	$sWhere= " and user_id=".$_SESSION['_id'] . " ";
}


if ($_GET['filtru']=='')
{
$oferte_query=mysql_query("select oferta.*,locatii.name from oferta left join locatii on oferta.id_loc=locatii.id where 1=1 $sWhere ORDER BY ID DESC ");
while ($oferte_array=mysql_fetch_array($oferte_query))
	{
	$img_rows=mysql_num_rows(mysql_query("select * from imagini where id_oferta='".$oferte_array['id']."'"));
	if ($img_rows>0) $oferte_array['src']=1;
	$oferta_[]=$oferte_array;
	}
}
else
{
$oferte_tot_query=mysql_query("select * from oferta where 1=1 $sWhere ORDER BY ID DESC ");
while ($oferte_tot_array=mysql_fetch_array($oferte_tot_query))
$result[]=$oferte_tot_array['id'];

///cautare dupa lista
if ($_GET['loc']!='')
	{
	//cautare nivel 0
	$oferte_lvl_0_query=mysql_query("select * from oferta where id_loc='".addslashes($_GET['loc'])."'");
	while ($oferte_lvl_0_array=mysql_fetch_array($oferte_lvl_0_query))
	$oferta_id[]=$oferte_lvl_0_array['id'];
	//cautare nivel 1
	$locatii_lvl_1_query=mysql_query("select * from locatii where id_parent='".addslashes($_GET['loc'])."'");
	while ($locatii_lvl_1_array=mysql_fetch_array($locatii_lvl_1_query))
	 { 
         $locatii_lvl_1[]=$locatii_lvl_1_array['id']; 
         //cautare nivel 2
             $locatii_lvl_2_query=mysql_query("select * from locatii where id_parent='".addslashes($locatii_lvl_1_array['id'])."'"); 
             while ($locatii_lvl_2_array=mysql_fetch_array($locatii_lvl_2_query)) 
             $locatii_lvl_1[]=$locatii_lvl_2_array['id']; 
     }
	
    
    if ($locatii_lvl_1!='')
	foreach ($locatii_lvl_1 as $id_loc)
	{
		$oferte_lvl_1_query=mysql_query("select * from oferta where id_loc='".addslashes($id_loc)."'");
		while ($oferte_lvl_1_array=mysql_fetch_array($oferte_lvl_1_query))
		$oferta_id[]=$oferte_lvl_1_array['id'];
		}
	if ($oferta_id!='')
	$result=array_intersect($result,$oferta_id);	

	if (($_GET['loc']!='') AND ($oferta_id==''))

	$result=array('a'=>'a');

	

	}



///cautare dupa tip

if (($_GET['tip']!='')AND($_GET['tip']!='0'))
{
	$oferte_tip_query=mysql_query("select * from oferta where tip='".addslashes($_GET['tip'])."' ORDER BY ID DESC");
	while ($oferte_tip_array=mysql_fetch_array($oferte_tip_query))
	$oferta_tip[]=$oferte_tip_array['id'];

	if ($oferta_tip!='')
	$result=array_intersect($result,$oferta_tip);
	if (($_GET['tip']!='') AND ($oferta_tip=='')AND ($_GET['tip']!='0'))
	$result=array('a'=>'a');
}



///cautare dupa cerere
if (($_GET['oferta']!='')AND($_GET['oferta']!='0'))
{
	$oferte_tip_query=mysql_query("select * from oferta where oferta='".addslashes($_GET['oferta'])."' ORDER BY ID DESC");
	while ($oferte_tip_array=mysql_fetch_array($oferte_tip_query))
	$oferta_cerere[]=$oferte_tip_array['id'];

	if ($oferta_cerere!='')
	$result=array_intersect($result,$oferta_cerere);
	if (($_GET['oferta']!='') AND ($oferta_cerere=='')AND ($_GET['oferta']!='0'))
	$result=array('a'=>'a');
}

///cautare dupa cuvinte
if (($_GET['cuvinte']!=''))
{
	$oferte_tip_query=mysql_query("select * from oferta WHERE  (detalii LIKE '%".addslashes($_GET['cuvinte'])."%'  OR detalii LIKE '%".addslashes($_GET['cuvinte'])."___ ' ) ORDER BY ID DESC" );
	while ($oferte_tip_array=mysql_fetch_array($oferte_tip_query))
	$oferta_cuvinte[]=$oferte_tip_array['id'];

	if ($oferta_cuvinte!='')
	$result=array_intersect($result,$oferta_cuvinte);
	if (($_GET['cuvinte']!='') AND ($oferta_cuvinte==''))
	$result=array('a'=>'a');
}

///cautare dupa cod

if ($_GET['cod']!='')
{
	$cod=str_replace('TM','',$_GET['cod']);
	$cod=str_replace('tm','',$cod);
	$oferte_tip_query=mysql_query("select * from oferta where id='".$cod."' ORDER BY ID DESC");
	while ($oferte_tip_array=mysql_fetch_array($oferte_tip_query))
	$oferta_cod[]=$oferte_tip_array['id'];


	if ($oferta_cod!='')
	$result=array_intersect($result,$oferta_cod);
	if (($_GET['cod']!='') AND ($oferta_cod==''))
	$result=array('a'=>'a');
}


if ( ($loc=='') AND ($_GET['pret']=='') AND ($_GET['cuvinte']=='') AND ($_GET['oferta']=='')AND ($_GET['tip']=='')   ) 
$result='';


if ($result!='') {	
	$result=array_unique($result);
	foreach($result as $oferta_id) {
		$oferta_query=mysql_query("select locatii.name,oferta.* from oferta left join locatii on oferta.id_loc=locatii.id  where oferta.id='".$oferta_id."' ORDER BY ID DESC ");
		while($oferta_array=mysql_fetch_array($oferta_query))	{
			$img_rows=mysql_num_rows(mysql_query("select * from imagini where id_oferta='".$oferta_array['id']."'"));
			if ($img_rows>0) $oferta_array['src']=1;
			$oferta_[]=$oferta_array;
	}	
}
}
}
  include "include/head.php";   
?>

<div id="container">
<? include "include/meniu.php"; ?> 

<div id="submenu"><div><div>
    <ul>
        <li ><a href="adauga_oferta.php">Adauga Oferta</a></li>
        <li id="off2"><a href="afisare_oferta.php">Vizualizare Oferte</a></li>
        
    </ul>
</div></div></div>
<div class="clear"></div>
<div id="content"><div id="mid">
<div>

<form enctype="multipart/form-data" action="afisare_oferta.php" method="GET" name="menu" >
<br class="clear"/>
<h2>FILTRE AFISARE</h2>
<div class="menu">
<ul>
    <li class="sub"><a href="#nogo"><input type="text" readonly name="nume_locatie" id="nume_locatie" value=zona class="tf b"/> </a>
<?=meniu('0'); ?> 
    </li>
</ul>
</div>
<input type="hidden" name="loc" id="loc" /> 

 <br>
    <br class="clear" />
    
   
    <label>Tip</label>
    <select name='tip' class='bb'>
    <? foreach ($tip as $key=>$value) 
        echo '<option value='.$key.'>'.$value.'</option> '; ?>
    </select>
    
    <br class="clear" />
    <label>Cerere</label>
     <select name='oferta' class='b'>
    <? foreach ($oferta as $key=>$value) 
        echo '<option value='.$key.'>'.$value.'</option> '; ?>
    </select>

    <br class="clear" />
    <label>Cuvinte Cheie</label>
    <input type="text"  name="cuvinte" id="nume_locatie" class="tf bb"/>
    
    <br class="clear" />
    <label>Cod Oferta</label>
    <input type="text"  name="cod" id="cod" class="tf s"/>
    
    <input type="hidden" name="filtru" value="1" />
<br class="clear" />
<br class="clear" />
<label>&nbsp;</label>
<input type="submit" value="ok" name="Cauta" class="ok"/>
</form>


<div>
<br class="clear" />
<br class="clear" />
<div class="cell s">ID</div>
<div class="cell bbb">ZONA</div>
<div class="cell bb">TIP</div>
<div class="cell m">OFERTA</div>
<div class="cell bb">PRET</div>
<div class="cell m ac">DETALII</div>
<div class="cell mm">POZE</div>
<br class="clear" />


<? $css=0;
     
    foreach ($oferta_ as $of) { 
    if ($css%2==0) $class='cell__'; else $class='cell_'; 
    if ($of['spof']==1)    $class='cell___';
    ?>
     <div class="<?=$class?> s" >
        <strong><?=$of['id']?></strong> </div>
     
     <div class="<?=$class?> bbb"  >
    <strong><?=$of['name']?></strong> </div>
    
     
     <div class="<?=$class?> bb" >
     <?=$tip[$of['tip']]?></div>
   
     <div class="<?=$class?> m" >
    <?=$oferta[$of['oferta']]?></div>

     <div class="<?=$class?> bb" >
        <?=$of['pret']?> <?=$valuta[$of['pret_tip']]?></div>

     <div class="<?=$class?> m ac" >
     <a href="" onclick="window.open('afiseaza_detalii.php?id=<?=$of['id']?>' ,'TIMONA_DESCRIERE','width=600,height=400,left=50,top=300,scrollbars=1');return false" ><img src="imagini/afiseaza.gif" /></a> </div>    

     <div class="<?=$class?> mm ac" >
     <? if ($of['src']!=''){?> <img src="imagini/photo2.gif" hspace="0" /><?}?></div>    
     
     
    
     <div class="cell_ " >
	<? if($_SESSION['_level']!=0) { ?> 
     <a href="modifica_oferta.php?modifica=<?=$of['id']?>"><img src="imagini/modifica.gif"  /></a>
      <? } ?>
     </div>
     
    <? if($_SESSION['level']==0) { ?> 
     <div class="cell_" >
     <a href="afisare_oferta.php?sterge=<?=$of['id']?>" onclick="return delete_confirm('Sunteri siguri ca vreti sa stergeti oferta?')"><img src="imagini/sterge.gif"/></a></div>
     <?}?>
     <br class="clear" />    
    
    <? $css++; }?>

</div>
</div>
</div>

<? include "include/footer.php";  ?> 



